package cn.math.appmath.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import cn.math.appmath.bean.DownloadBean;
import cn.math.appmath.biz.DownloadBiz;
import cn.math.appmath.entity.Download;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
	private static final long serialVersionUID = -4565898601842133546L;
	private DownloadBiz downloadBiz;
	private DownloadBean downloadBean;
	private String message;
	private Integer[] nos;
	
	//上传文件的files对象
	private File file;
	//上传文件名
	private String fileFileName;
	//上传文件的MIME类型
	private String fileContentType;
	//上传文件的描述信息
	private String descript;
	//上传文件所属类型
	private Integer type;
	//保存上传文件的目录，相对于Web应用程序的根路径，在struts.xml文件中配置
	private String uploadDir;
	
	/**
	 * 文件上传Action处理方法
	 * @return
	 * @throws Exception
	 */
	public String uploadFile() throws Exception{
		try{
			String newFileName = null;
			long now = new Date().getTime();
			
			//得到保存上传文件的目录的真实路径
			String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
			
			File dir = new File(path);
			//如果这个目录不存在，则创建它
			if(!dir.exists())
				dir.mkdir();
			
			int index = fileFileName.lastIndexOf('.');
			//判断上传文件名是否有扩展名，以时间戳作为新的文件名
			if(index != -1)
				newFileName = now + fileFileName.substring(index);
			else
				newFileName = Long.toString(now);
			
			BufferedOutputStream bos = null;
			BufferedInputStream bis = null;
			
			//读取保存在临时目录下的上传文件，写入到新的文件中
			try{
				FileInputStream fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				
				FileOutputStream fos = new FileOutputStream(new File(dir,newFileName));
				bos = new BufferedOutputStream(fos);
				
				byte[] buf = new byte[4096];
				
				int len = -1;
				while((len = bis.read(buf)) != -1){
					bos.write(buf, 0, len);
				}
			}catch(IOException e){
				e.printStackTrace();
			}finally{
				try{
					if(null != bis)
						bis.close();
				}catch(IOException e){
					e.printStackTrace();
				}
				
				try{
					if(null != bos)
						bos.close();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
			//将文件信息保存到数据库
			Download download = new Download();
			download.setDname(descript);
			download.setDtype(type);
			download.setDurl(uploadDir + "/" + newFileName);
			if(downloadBiz.save(download))
				message = "文件上传成功";
				return SUCCESS;
		}catch (Exception ex){
			ex.printStackTrace();
			message = "文件上传失败";
			return INPUT;
		}
	}

	public String downloadforward(){
		
		message = "";
		return SUCCESS;
	}
	
	/**
	 * 查找符合条件的下载信息
	 * @return
	 */
	public String findDownload(){
		String pageStr = ServletActionContext.getRequest().getParameter("page");
		int page = 0;
		if(pageStr != null && !"".equals(pageStr))
			page = Integer.parseInt(pageStr);
		String param = ServletActionContext.getRequest().getParameter("param");
		if(param == null || param.equals(""))
			param = "backward";
		String typeStr = ServletActionContext.getRequest().getParameter("type");
		type = 1;
		if(typeStr != null && !"".equals(typeStr))
			type = Integer.parseInt(typeStr);
		downloadBean = downloadBiz.findAll(page, param, type);
		if(type == 2)
			return "regulation";
		else if(type == 3)
			return "plan";
		else if(type == 4)
			return "download";
		else
			return "all";		
	}	
	
	/**
	 * 删除下载信息
	 * @return
	 */
	public String deleteDownload(){
		String noStr = ServletActionContext.getRequest().getParameter("no");
		if(noStr != null && !"".equals(noStr)){
			Integer no = new Integer(noStr);
			nos = new Integer[]{no};
		}
		if(this.downloadBiz.delete(nos)){
			return SUCCESS;
		}else{
			message = "删除下载信息失败";
			return INPUT;
		}
	}
	
	public DownloadBean getDownloadBean() {
		return downloadBean;
	}

	public void setDownloadBean(DownloadBean downloadBean) {
		this.downloadBean = downloadBean;
	}

	public void setDownloadBiz(DownloadBiz downloadBiz) {
		this.downloadBiz = downloadBiz;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getDescript() {
		return descript;
	}

	public void setDescript(String descript) {
		this.descript = descript;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}
}
